package com.gy.paidao.person;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.TextPaint;
import android.text.TextUtils;
import android.text.method.LinkMovementMethod;
import android.text.style.BackgroundColorSpan;
import android.text.style.ClickableSpan;
import android.text.style.URLSpan;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.gy.paidao.BaseActivity;
import com.gy.paidao.MyApplication;
import com.gy.paidao.R;
import com.gy.paidao.bean.BaseBean;
import com.gy.paidao.util.AnimUtil;
import com.gy.paidao.util.KeyBoard;
import com.gy.paidao.util.LogUtil;
import com.gy.paidao.util.Urls;
import com.jky.struct2.http.core.AjaxParams;

import org.json.JSONException;
import org.json.JSONObject;

/**
 * 作者：gy on 16/2/20
 * 说明：
 */
public class RegisterActivity extends BaseActivity {

    private EditText etName,etPassword;
    private TextView tvLogin,tvRegister,tvClause;//条款
    private String strName,strPassword;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register_layout);
        setViews();
    }

    @Override
    protected void initVariable() {

    }

    @Override
    protected void setTitleViews() {

    }

    @Override
    protected void setViews() {
        etName = (EditText) findViewById(R.id.activity_register_et_name);
        etPassword = (EditText) findViewById(R.id.activity_register_et_password);
        tvLogin = (TextView) findViewById(R.id.activity_register_tv_login);
        tvRegister = (TextView) findViewById(R.id.activity_register_tv_register);
        tvClause = (TextView) findViewById(R.id.activity_register_tv_clause);

        tvLogin.setOnClickListener(this);
        tvRegister.setOnClickListener(this);


        setClause();
    }

    /** 设置阅读条款*/
    private void setClause(){
        String clause = this.getResources().getString(R.string.activity_register_agree);
        int termsStart = 0,termsEnd = 0,privacyStart = 0,privacyEnd =0;
        if("zh".equals(MyApplication.language)){//中文
            //用户在建立账户前已同意相关条款并阅读了隐私政策
            termsStart = clause.indexOf("同意");
            termsEnd = clause.indexOf("并");
            privacyStart = clause.indexOf("了");
            privacyEnd = clause.indexOf(".");
        } else if("en".equals(MyApplication.language)){//英文
            termsStart = clause.indexOf("Terms");
            termsEnd = clause.indexOf(" and have");
            privacyStart = clause.indexOf("Privacy");
            privacyEnd = clause.indexOf(".");
        }
        SpannableString spannableString1 = new SpannableString(clause);
        spannableString1.setSpan(new ClickableSpan() {

            @Override
            public void updateDrawState(TextPaint ds) {
                super.updateDrawState(ds);
                ds.setUnderlineText(false);//设置超链
                ds.setColor(Color.parseColor("#ffcdcdcd"));//设置超链字体颜色

            }

            @Override
            public void onClick(View widget) {
                //设置超链点击事件
                LogUtil.e("使用条款");
            }
        },termsStart,termsEnd, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

        spannableString1.setSpan(new ClickableSpan() {

            @Override
            public void updateDrawState(TextPaint ds) {
                super.updateDrawState(ds);
                ds.setUnderlineText(false);//设置超链
                ds.setColor(Color.parseColor("#ffcdcdcd"));//设置超链字体颜色

            }

            @Override
            public void onClick(View widget) {
                //设置超链点击事件
                LogUtil.e("隐私策略");
            }
        }, privacyStart, privacyEnd, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        tvClause.setText(spannableString1);
        tvClause.setMovementMethod(LinkMovementMethod.getInstance());
    }

    @Override
    protected void doClickAction(int viewId) {
        super.doClickAction(viewId);
        switch (viewId){
            case R.id.activity_register_tv_register:
                strName = etName.getText().toString().trim();
                strPassword = etPassword.getText().toString().trim();
                if(TextUtils.isEmpty(strName) || TextUtils.isEmpty(strPassword)){
                    showToast(R.string.activity_login_input_error);
                } else {
                    KeyBoard.demissKeyBoard(this,etName);
                    requestRegister();
                }
                break;

            case R.id.activity_register_tv_login:
                this.finish();
                AnimUtil.pushRightInAndOut(this);
                break;
        }
    }

    private void requestRegister(){
        showLoading();
        params = new AjaxParams();
        params.put("email",strName+"");
        params.put("password",strPassword+"");
        httpRequest.get(Urls.url_register,params,callBack,0);
    }

    @Override
    protected void handlerBean(int requestCode, BaseBean baseBean) {
        super.handlerBean(requestCode, baseBean);
        LogUtil.e(baseBean.toString());
        if(baseBean.success){
            try {
                JSONObject object = new JSONObject(baseBean.data.toString());
                int userId = object.optInt("userId");
                intent = new Intent(this,RegisterEmailActivity.class);
                intent.putExtra("userId",userId);
                intent.putExtra("email",strName);
                startActivityForResult(intent,101);
            } catch (JSONException e) {
                e.printStackTrace();
            }

        } else {
            if("0010002".equals(baseBean.errCode)){
                //this.finish();
            }
            if(!TextUtils.isEmpty(baseBean.errMsg)){
                showToast(baseBean.errMsg);
            }
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(resultCode == 101){
            setResult(101);
            finish();
        }
    }
}
